﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace DocumentGen
{
    public class Program
    {
        public static string generateNav(JArray contents)
        {
            string res = @"
<nav aria-hidden=""true"" class=""menu"" id=""menu"" tabindex=""-1"">
		<div class=""menu-scroll"">
			<div class=""menu-content"">
				<a class=""menu-logo"" href=""/"">LonAC NOI大纲拆解学习</a>
				<ul class=""nav"">
					<li>";
            foreach (var x in contents) // 一级目录
            {
                res +=
                    $"<a class=\"\"collapsed waves-attach waves-effect\"\" data-toggle=\"\"collapse\"\" href=\"\"#{x["name"]}\"\">{x["name"].ToString()}</a>	<ul class=\"\"menu-collapse collapse\"\" id=\"\"{x["name"].ToString()}\"\">";
                foreach (var y in x["items"])
                {
                    string path = y["path"].ToString().Replace(".md", ".html");

                    res +=
                        $"<li class=\\\"active\\\">\n\t\t\t\t\t\t\t\t\t<a class=\\\"waves-attach\\\" href=\\\"/{path}\\\">{y["name"].ToString()}</a>\n\t\t\t\t\t\t\t\t</li>";
                }

                res += "</ul>";
            }

            res += @"</li>
					
				</ul>
			</div>
		</div>
	</nav>";
            return res;
        }

        public static string MdConvert(string filePath)
        {
            string md_contents = File.ReadAllText(filePath);
            return IF_MarkDown.ToHtml(md_contents);
        }

        public static void Main()
        {
            //var rootpath = AppDomain.CurrentDomain.BaseDirectory;

            //Console.WriteLine(rootpath);


            string json_contents = File.ReadAllText("../../../mdconfig.json");
            var contents = JArray.Parse(json_contents);
            string nav_html = generateNav(contents);
            string index_md = File.ReadAllText("../../index.md");

            File.WriteAllText("../../../html/index.html", HtmlTemplate.HtmlConvert("首页", nav_html, IF_MarkDown.ToHtml(index_md)));
             

            foreach (var x in contents) // 一级目录
            {
                foreach (var y in x["items"])
                {
                    var md_file_path = "../../" + y["path"].ToString();

                    string md = File.ReadAllText(md_file_path);

                    File.WriteAllText("../../../html/" + y["path"].ToString().Replace(".md", ".html"), HtmlTemplate.HtmlConvert(y["name"].ToString(), nav_html, IF_MarkDown.ToHtml(md)));

                }
            }

            Console.WriteLine("转换完毕");
        }
    }
}